package cn.exrick.manager.controller;


import cn.exrick.common.pojo.DataTablesResult;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.pojo.TbStock;
import cn.exrick.manager.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;


@RestController
@Api(description= "库存管理")
public class StockController {

    @Autowired
    private StockService stockService;

    @RequestMapping(value = "/stock/list",method = RequestMethod.GET)
    @ApiOperation(value = "获取库存记录列表")
    public DataTablesResult getStockList(int draw, int start, int length, TbStock tbStock,
                                         @RequestParam("order[0][column]") int orderCol, @RequestParam("order[0][dir]") String orderDir){

        //获取客户端需要排序的列
        String[] cols = {"checkbox","s.stock", "s.item_id","s.num", "s.price", "s.inbound_date"};
        String orderColumn = cols[orderCol];
        //默认排序列
        if(orderColumn == null) {
            orderColumn = "s.inbound_date";
        }
        //获取排序方式 默认为desc(asc)
        if(orderDir == null) {
            orderDir = "desc";
        }

        DataTablesResult result= stockService.getStockList(draw,start,length,tbStock,orderColumn,orderDir);
        return result;
    }

    @RequestMapping(value = "/stock/del/{ids}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除库存记录")
    public Result<Object> delStock(@PathVariable Long[] ids){
        stockService.deleteStock(Arrays.asList(ids));
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/stock/update",method = RequestMethod.POST)
    @ApiOperation(value = "更新库存记录")
    public Result<Object> updateStock(TbStock tbStock){
        stockService.updateStock(tbStock);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/stock/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增库存记录")
    public Result<Object> addStock(TbStock tbStock){
        stockService.addStock(tbStock);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/stock/{id}",method = RequestMethod.GET)
    public Result<Object> getStock(@PathVariable Long id){
        TbStock tbStock = stockService.getStockById(id);
        return new ResultUtil<Object>().setData(tbStock);
    }
}
